import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';

import 'package:get/get.dart';
import 'package:grouped_list/grouped_list.dart';
import 'package:smart_translator/app/style/widget_style.dart';
import 'package:smart_translator/app/utils/CommonUtil.dart';
import 'package:smart_translator/app/widgets/CommonWidgets.dart';

import '../../../style/app_colors.dart';
import '../../../widgets/AppHeader.dart';
import '../controllers/language_opinion_controller.dart';

class LanguageOpinionView extends GetView<LanguageOpinionController> {
  const LanguageOpinionView({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Stack(
          children: [
            Container(
              child: SvgPicture.asset(
                "assets/images/header_part_bg.svg",
                width: Get.width,
                fit: BoxFit.fitWidth,
              ),
            ),
            AppHeader.obtainHeaderWithoutRightPart('输入语言'),
            Container(
              width: Get.width,
              margin: EdgeInsets.only(top: 60.h),
              padding: EdgeInsets.all(20.w),
              child: Obx(()=>ListView(
                children: [
                  Container(
                    height: 40.h,
                    padding: EdgeInsets.only(left: 10.w),
                    decoration: BoxDecoration(
                      color: AppColors.swith_inactive_bg_color,
                      borderRadius: BorderRadius.circular(8), // Optional: add border radius
                    ),
                    child: TextField(
                      decoration: InputDecoration(
                        border: InputBorder.none,
                        focusedBorder: InputBorder.none,
                        contentPadding:EdgeInsets.only(left: 10.w),
                        prefixIcon: SizedBox(
                          child: Container(
                            child: Image.asset('assets/images/icon_searchOrReplace_searching.png'),
                          ),
                          width: 16.w,
                          height: 16.h,
                        ),
                        prefixIconConstraints: BoxConstraints(
                          minWidth: 16.w, // 最小宽度
                          minHeight: 16.h, // 最小高度
                        ),
                        hintText: '搜索语言',
                        hintStyle: TextStyle(
                            fontSize: 14.sp
                        ),

                      ),

                      onChanged: (v) {
                        controller.searchContent(v);
                      },

                    ),
                  ),
                  SizedBox(height: 16.h,),
                  controller.searchMode.value ? Column(
                    children: controller.searchResult.value.map((item) {
                      return InkWell(
                        onTap: () {
                            controller.selectedLangue(item);
                        },
                        child: Container(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(item['desc'])
                                ],
                              ),
                              SizedBox(height: 17.h,),
                              CommonWidgets.getMyDivider(),
                              SizedBox(height: 17.h,)
                            ],
                          ),
                        ),
                      );
                    }).toList(),
                  ): Column(
                    children: [
                      Row(
                        children: [
                          Text('最近', style: AppStyleThemeData.normal_bold,),
                        ],
                      ),
                      SizedBox(height: 8.h,),

                      Container(
                        padding: EdgeInsets.all(16.w),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(8),
                          boxShadow: [
                            CommonWidgets.getBoxShadow(),
                          ],// Optional: add border radius
                        ),
                        child: Column(
                          children: controller.generateRecentLangs(),
                        ),
                      ),
                      SizedBox(height: 20.h,),
                      Row(
                        children: [
                          Text('全部', style: AppStyleThemeData.normal_bold,),
                        ],
                      ),
                      SizedBox(height: 20.h,),
                      Container(
                        padding: EdgeInsets.all(16.w),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(8),
                          boxShadow: [
                            CommonWidgets.getBoxShadow(),
                          ],// Optional: add border radius
                        ),
                        child: Column(
                          children: controller.languagesMap.value.keys.map((itemKey) {
                            List<dynamic> items = controller.languagesMap.value[itemKey];
                            return Container(
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  // Text(itemKey),
                                  Column(
                                    children: items.map((item)=>
                                        InkWell(
                                          onTap: () {
                                              controller.selectedLangue(item);
                                          },
                                          child: Container(
                                            child: Column(
                                              crossAxisAlignment: CrossAxisAlignment.start,
                                              children: [
                                                (controller.selectedLanguageCode != null && controller.selectedLanguageCode == item['code'])? Text(item['desc'],style: TextStyle(color: AppColors.app_base_theme_color),):
                                                Text(item['desc']),
                                                SizedBox(height: 17.h,),
                                                CommonWidgets.getMyDivider(),
                                                SizedBox(height: 17.h,)
                                              ],
                                            ),
                                          ),
                                        )
                                    ).toList(),
                                  )

                                ],
                              ),

                            );
                          }).toList(),
                        ),
                      )
                    ],
                  )



                ],
              )),
            )
          ],
        )
    );
  }
}
